package com.ryskoo.permission.vendormanager.service;

import com.ryskoo.framework.core.bean.BasicPO;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import com.ryskoo.framework.core.utils.DateFormatHelper;
import com.ryskoo.framework.core.utils.MD5Util;
import com.ryskoo.permission.managerrolerel.domain.ManagerRoleRelPO;
import com.ryskoo.permission.managerrolerel.repository.IManagerRoleRelRepository;
import com.ryskoo.permission.role.domain.RolePO;
import com.ryskoo.permission.vendormanager.domain.StoreManagerPO;
import com.ryskoo.permission.vendormanager.repository.IVendorManagerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Shawn
 */
@Service
public class VendorManagerServiceImpl extends BasicServiceImpl<StoreManagerPO> implements IVendorManagerService {
	@Autowired
    private IVendorManagerRepository managerRepository;
    
	@PostConstruct
	public void init() {
		this.setDao(managerRepository);
	}

	@Autowired
    private IManagerRoleRelRepository managerRoleRelRepository;

	/**
	 * 根据用户名查询管理员信息
	 * @param userName
	 * @return
	 */
	@Override
	public StoreManagerPO getManagerByLoginName(String userName) {
		return managerRepository.getManagerByLoginName(userName);
	}

	/**
	 * 新增管理员信息以及管理员和角色的关联关系
	 *
	 * @param manager 管理员信息 {@link StoreManagerPO}
	 */
	@Override
    @Transactional(rollbackFor = Exception.class)
	public void createManagerAndRole(StoreManagerPO manager) {
        manager.setCreateTime(DateFormatHelper.getNowTimeStr());
        manager.setPassword(MD5Util.md5(manager.getLoginName() + manager.getPassword()));
        super.create(manager);
        // 保存管理员和角色关联关系
        saveManagerAndRoleRel(manager);
	}

	/**
	 * 编辑管理员信息以及管理员和角色的关联关系
	 *
	 * @param manager 管理员信息 {@link StoreManagerPO}
	 */
	@Override
	public void updateManagerAndRole(StoreManagerPO manager) {
        manager.setPassword(MD5Util.md5(manager.getPassword()));
        super.update(manager);
        // 先删除之前的管理员和角色关联关系
        managerRoleRelRepository.deleteRelByManagerId(manager.getUuid());
        // 再保存编辑后的管理员和角色关联关系
        saveManagerAndRoleRel(manager);
	}

	private void saveManagerAndRoleRel(StoreManagerPO manager) {
        List<ManagerRoleRelPO> managerRoleRelList = new ArrayList<>();
        if (manager.getRoleList() != null && manager.getRoleList().size() > 0) {
        	for (RolePO role : manager.getRoleList()) {
        		ManagerRoleRelPO managerRoleRel = new ManagerRoleRelPO();
        		managerRoleRel.setUuid(BasicPO.genUuid());
        		managerRoleRel.setManagerId(manager.getUuid());
        		managerRoleRel.setRoleId(role.getUuid());
        		managerRoleRelList.add(managerRoleRel);
        	}
        	managerRoleRelRepository.batchCreate(managerRoleRelList);
        }
    }

	/**
	 * 重写create方法，把管理员密码加密
	 */
	@Override
	public void create(StoreManagerPO m) {
		m.setCreateTime(DateFormatHelper.getNowTimeStr());
		m.setPassword(MD5Util.md5(m.getPassword()));
		super.create(m);
	}
	
}
